package com.example.administrator.reader.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.reader.R;
import com.example.administrator.reader.activity.CitySearch;
import com.example.administrator.reader.activity.MoreBookActivity;
import com.example.administrator.reader.element.CGdElement;
import com.example.administrator.reader.element.CGridElement;
import com.example.administrator.reader.element.CVpElement;
import com.example.administrator.reader.element.CZhanElement;
import com.example.administrator.reader.element.CZhudaElement;
import com.example.administrator.reader.entity.City;
import com.example.administrator.reader.request.BaseRequest;
import com.example.administrator.reader.request.CityRequest;
import com.example.administrator.reader.utils.Constants;
import com.example.administrator.reader.utils.MyToast;
import com.example.administrator.reader.utils.UIUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;


/**
 * Created by Administrator on 2016-09-04.
 */
public class CityFragment extends BaseFragment {
    @ViewInject(R.id.fl_vp)
    FrameLayout fl_vp;
    @ViewInject(R.id.fl_gv_icon)
    FrameLayout fl_gv_icon;
    @ViewInject(R.id.fl_zhuda)
    FrameLayout fl_zhuda;
    @ViewInject(R.id.fl_zhan)
    FrameLayout fl_zhan;
    @ViewInject(R.id.fl_gd)
    FrameLayout fl_gd;
    @ViewInject(R.id.srl)
    SwipeRefreshLayout srl;

    @ViewInject(R.id.tv_zhuda_more)
    private TextView tv_zhuda_more;
    @ViewInject(R.id.tv_wenxue_more)
    private TextView tv_wenxue_more;

    CityRequest request;
    String url = "city.shtml";

    // FIXME
    public static City cityData;

    CVpElement cVpElement;
    CGridElement cGridElement;
    // FIXME
    static CZhudaElement cZhudaElement;
    CGdElement cGdElement;
    CZhanElement cZhanElement;

    @Override
    protected void setUpContentView() {
        view = View.inflate(UIUtils.getContext(), R.layout.f_city, null);
        ViewUtils.inject(this, view);
    }

    @Override
    protected void setUpView() {

    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {

        // 网络请求
        requestFunc();

        // 下拉刷新
        srlFunc();

        // 元素的实例化与添加
        elementFunc();

        // 点击监听
        clickListener();
    }

    @Override
    public void loadData() {
        request.firstLoad(url);
    }

    @Override
    public void reloadData() {
        request.refreshLoad(url);
    }



    private void elementFunc() {
        /**
         * 1、实例化不同的元素
         * 2、且把不同的元素加进相应的FrameLayout
         */
        cVpElement = new CVpElement(getActivity());
        fl_vp.addView(cVpElement.getView());
        cGridElement = new CGridElement(getActivity());
        fl_gv_icon.addView(cGridElement.getView());
        cZhudaElement = new CZhudaElement(getActivity());
        fl_zhuda.addView(cZhudaElement.getView());
        cGdElement = new CGdElement(getActivity());
        fl_gd.addView(cGdElement.getView());
        cZhanElement = new CZhanElement();
        fl_zhan.addView(cZhanElement.getView());
    }


    private void srlFunc() {
        srl.setColorSchemeResources(R.color.red,
                R.color.green,
                R.color.blue);
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread() {
                    @Override
                    public void run() {
                        //刷新：其余都不动，只是（设置缓存），重新填充数据
                        reloadData();
                    }
                }.start();
            }
        });
    }

    private void requestFunc() {
        request = new CityRequest();
        request.setInter(new BaseRequest.BaseInter<City>() {
            @Override
            public void handleData(City data) {
                cityData = data;
                Message msg = Message.obtain();
                if (cityData == null) {
                    msg.what = Constants.ERROR;
                } else {
                    msg.what = Constants.OK;
                }
                handler.sendMessage(msg);
            }
        });
    }

    private void clickListener() {

        cGridElement.getGridView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), MoreBookActivity.class);
                switch (position) {

                    case 0:
                        intent.putExtra("TAG", "热门");
                        startActivity(intent);
                        break;
                    case 1:
                        intent.putExtra("TAG", "精品");
                        startActivity(intent);
                        break;
                    case 2:
                        intent.putExtra("TAG", "推荐");
                        startActivity(intent);
                        break;
                    case 3:
                        intent.putExtra("TAG", "免费");
                        startActivity(intent);
                        break;

                }

            }
        });
    }

    /**
     * 1、加载数据
     * 2、每个元素关联相应的Adapter
     */
    private void fillAllElement() {
        cVpElement.loadData(cityData.getLunbos());
        cVpElement.fillView();
        cGridElement.loadData(null);
        cGridElement.fillView();
        cZhudaElement.loadData(cityData.getZhudas());
        cZhudaElement.fillView();
        cGdElement.loadData(cityData.getGudians());
        cGdElement.fillView();
        cZhanElement.loadData(cityData.getZhanzhengs());
        cZhanElement.fillView();
    }

    @OnClick({R.id.tv_zhuda_more, R.id.tv_wenxue_more})
    public void onClick(View view) {
        Intent intent = new Intent(getActivity(), MoreBookActivity.class);
        switch (view.getId()) {
            case R.id.tv_zhuda_more:
                intent.putExtra("TAG", "主打");
                startActivity(intent);
                break;
            case R.id.tv_wenxue_more:
                intent.putExtra("TAG", "文学");
                startActivity(intent);
                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constants.ERROR:
                    srl.setRefreshing(false);
                    break;
                case Constants.OK:
                    fillAllElement();
                    srl.setRefreshing(false);
                    break;
            }
        }
    };

}
